<?php

class Dashboard {
	
	public $dashboard_id;
	public $client_id;
	public $user_id;
	public $tip;
	public $tara_plecare;
    public $oras_plecare;
	public $data_plecare;
	
	public $tara_sosire;
	public $oras_sosire;
	public $data_sosire;
	
	public $comentariu;
	public $date_added;
	
	public $nume_user;
	public $prenume_user;
    public $pret_total;
	public $moneda;
	public $km;
  
	public function __construct($client_id, $user_id, $tip, $tara_plecare = null, $oras_plecare = null, $data_plecare = null, $tara_sosire = null, $oras_sosire = null, $data_sosire = null, $comentariu = null, $pret_total = null, $km = null, $moneda = null) {
		
		$this->client_id = $client_id;
		$this->user_id = $user_id;
		$this->tip = $tip;
        $this->tara_plecare = $tara_plecare;
		$this->oras_plecare = $oras_plecare;        
		$this->data_plecare = $data_plecare;
        $this->tara_sosire = $tara_sosire;
		$this->oras_sosire = $oras_sosire;
		$this->data_sosire = $data_sosire;
		$this->comentariu = $comentariu;	
        $this->pret_total = $pret_total;
		$this->moneda = $moneda;
		$this->km = $km;
	
	}
	
	public function queryInsert($db) {
		
		$query = $db->prepare("INSERT INTO `dashboard`  ( 	`client_id` ,			`user_id` , 
															`tip` ,					`tara_plecare`,
															`oras_plecare` ,		`data_plecare`,
	                                						`tara_sosire` ,  		`oras_sosire` , 	    
	                                						`data_sosire`,			`comentariu`,
	                                						`date_added`,			`pret_total`,
	                                						`moneda`,               `km`) 		
                                                                                        		            		
                    					VALUES (			:client_id, 		      :user_id, 
		            										:tip, 			     	  :tara_plecare,
		            										:oras_plecare, 			  :data_plecare,
	                      									:tara_sosire, 			  :oras_sosire,		  
	                      									:data_sosire,			  :comentariu,
	                      									NOW(),					  :pret_total,
	                      									:moneda,                  :km				
		                      			)");
		
		$query->bindParam(':client_id', 		($this->client_id));		
		$query->bindParam(':user_id', 			($this->user_id));
		$query->bindParam(':tip', 				($this->tip));
		
		$query->bindParam(':tara_plecare', 		($this->tara_plecare), PDO::PARAM_STR);
		$query->bindParam(':oras_plecare', 		($this->oras_plecare), PDO::PARAM_STR);
		$query->bindParam(':data_plecare', 		($this->data_plecare));
		$query->bindParam(':tara_sosire', 		($this->tara_sosire), PDO::PARAM_STR);

		$query->bindParam(':oras_sosire', 		($this->oras_sosire), PDO::PARAM_STR);
		$query->bindParam(':data_sosire', 		($this->data_sosire));
		$query->bindParam(':comentariu', 		($this->comentariu), PDO::PARAM_STR);
        $query->bindParam(':pret_total',        ($this->pret_total), PDO::PARAM_STR);
		$query->bindParam(':moneda', 			($this->moneda), PDO::PARAM_STR);
		$query->bindParam(':km', 				($this->km), PDO::PARAM_STR);				
		                
		try {		
			if ($query->execute() === FALSE) {
				$errInfo = $query->errorInfo();
				$errInfo = $errInfo[2];
				var_dump($errInfo);
				$query->closeCursor();	
				throw new QueryInsertDashboardException("Error inserting dashboard " . $errInfo);
			}
							
			$this->dashboard_id = (int) $db->lastInsertId();		
			$query->closeCursor();
			return $this->user_id; 
		}
		catch (QueryInsertDashboardException $e) {			
			$query->closeCursor();
			throw new QueryInsertDashboardException("Error inserting into Dashboard: " . $errInfo);
		}
	}	
	
	public static function getActivity($db, $client_id, $tip) {
		$query = $db->prepare("SELECT d.*, user.nume, user.prenume 
                    FROM dashboard d
                    LEFT JOIN user ON user.id = d.user_id 
                    WHERE d.client_id = :client_id
                    AND d.tip = :tip");
                    
        $query->bindParam(':client_id', $client_id);
        $query->bindParam(':tip', $tip);
        
        if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
            $query->closeCursor();
            throw new QueryLoadDashboardException("Failed loading dasboard activity for client " . $client_id . $errInfo);
        }
                        
        $arrDash = array();		
		
 		while ($data = $query->fetch(PDO::FETCH_ASSOC)) {
 			$dash = new Dashboard($client_id, $data['user_id'], $tip, $data['tara_plecare'], $data['oras_plecare'], $data['data_plecare'], $data['tara_sosire'], $data['oras_sosire'], $data['data_sosire'], $data['comentariu'], $data['pret_total'], $data['km'], $data['moneda']);
 			$dash->dashboard_id = $data['dashboard_id'];
 			$dash->nume_user = $data['nume'];
 			$dash->prenume_user = $data['prenume'];
 			$dash->date_added = $data['date_added'];
 			$arrDash[] = $dash;
 		}
 		return $arrDash;
	}
	
	public static function queryDeleteByID($db, $client_id, $id) {
		
		$query = "DELETE FROM dashboard WHERE dashboard_id = $id AND client_id = $client_id";

		if (($result = $db->query($query)) === FALSE) {	
            $errInfo = $db->errorInfo();
            $errInfo = $errInfo[2];
           	throw new QueryDeleteDashboardException("Failed deleting from dash with dash id " . $id);
		}       	
		
		return CT_RESULT_OK;  
	}
	
	public static function queryGetByID($db, $client_id, $id) {
		$query = $db->prepare("SELECT d.*, user.nume, user.prenume 
                    FROM dashboard d
                    LEFT JOIN user ON user.id = d.user_id 
                    WHERE d.client_id = :client_id
                    AND d.dashboard_id = :id
                    LIMIT 1");
                    
        $query->bindParam(':client_id', $client_id);
        $query->bindParam(':id', $id);
        
        if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
            $query->closeCursor();
            throw new QueryLoadDashboardException("Failed loading dasboard with id " . $id . $errInfo);
        }
        
 		$data = $query->fetch(PDO::FETCH_ASSOC);
 		$dash = new Dashboard($client_id, $data['user_id'], $tip, $data['tara_plecare'], $data['oras_plecare'], $data['data_plecare'], $data['tara_sosire'], $data['oras_sosire'], $data['data_sosire'], $data['comentariu'], $data['pret_total'], $data['km'], $data['moneda']);
 		$dash->dashboard_id = $data['dashboard_id'];
 		$dash->nume_user = $data['nume'];
 		$dash->prenume_user = $data['prenume'];
 		$dash->date_added = $data['date_added']; 		
 		return $dash; 
	}	
	
	public function queryUpdate($db, $client_id, $id) {	
	    
        $query = $db->prepare("UPDATE `dashboard`  
                                SET
                                    `tara_plecare` = :tara_plecare,
                                    `oras_plecare` = :oras_plecare,
                                    `data_plecare` = :data_plecare,
                                    `tara_sosire` = :tara_sosire,
                                    `oras_sosire` = :oras_sosire,
                                    `data_sosire` = :data_sosire,
                                    `comentariu` = :comentariu,
                                    `pret_total` = :pret_total,
                                    `moneda` = :moneda,
                                    `km` = :km                        
                                WHERE 
                                    client_id = :client_id
                                    AND dashboard_id = :id
                                LIMIT 1");
        
        $query->bindParam(':client_id',         ($this->client_id));        
        $query->bindParam(':id',         ($this->dashboard_id));        
        
        $query->bindParam(':tara_plecare',         ($this->tara_plecare), PDO::PARAM_STR);
        $query->bindParam(':oras_plecare',         ($this->oras_plecare), PDO::PARAM_STR);
        $query->bindParam(':data_plecare',         ($this->data_plecare));
        $query->bindParam(':tara_sosire',         ($this->tara_sosire), PDO::PARAM_STR);
        $query->bindParam(':oras_sosire',         ($this->oras_sosire), PDO::PARAM_STR);
        $query->bindParam(':data_sosire',         ($this->data_sosire));
        $query->bindParam(':comentariu',         ($this->comentariu));
        $query->bindParam(':pret_total',         ($this->pret_total));
        $query->bindParam(':moneda',         ($this->moneda));
        $query->bindParam(':km',                 ($this->km));                
                        
        if ($query->execute() === FALSE) {    

            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
            $query->closeCursor();
            throw new QueryUpdateDashboardException("Failed updating dashboard - owner's id: " . $this->dashboard_id . $errInfo);
        }
                  
        //close the cursor
        $query->closeCursor();
          
		return CT_RESULT_OK; 
	}
}

?>
